package com.qf.controller;

import com.qf.pojo.Emp;
import com.qf.service.EmpService;
import com.qf.service.EmpServiceImpl;
import jdk.nashorn.internal.ir.RuntimeNode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 修改员工信息
 * @author 千锋健哥
 */
@WebServlet("/update")
public class UpdateEmpController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 接收表单中的所有数据
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String salary = req.getParameter("salary");
        String age = req.getParameter("age");

        //2. 封装实体对象
        Emp emp = new Emp();
        emp.setId(Integer.parseInt(id));
        emp.setName(name);
        emp.setSalary(Double.parseDouble(salary));
        emp.setAge(Integer.parseInt(age));

        //3. 调用service保存修改
        EmpService empService = new EmpServiceImpl();
        empService.updateEmp(emp);

        //4. 跳转到列表页面, 刷新员工列表, 跳转到员工列表servlet中
        resp.sendRedirect("/empList");
    }

}
